package ctrl_touriste;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import race.RaceManager;
import royaume.ActionsPeriodiques;
import royaume.RoyaumeManager;
import utilisateur.SeigneurManager;


/**
 * Servlet implementation class ServletAccueil
 */
public class ServletAccueil extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String defaultPage;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletAccueil() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// ctx = config.getServletContext();
		defaultPage = config.getInitParameter("defaultPage");
		new ActionsPeriodiques();
		new RaceManager();
		new RoyaumeManager();
		new SeigneurManager();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pageName;
		HttpSession session = request.getSession(true);
		if (request.getAttribute("content") == null
				|| request.getAttribute("content").equals("null")
				|| request.getAttribute("content").equals("")) {
			if (session.getAttribute("user") != null)
				pageName = "royaume.jsp";
			else
				pageName = defaultPage;
		} else {
			pageName = request.getAttribute("content").toString();
		}
		request.setAttribute("content", pageName);
		getServletContext().getRequestDispatcher("/JSP/index.jsp").forward(
				request, response);
	}

}
